library sampletex;

uses
  SysUtils,
  Classes,
  GSPluginIntf in '..\GSPluginIntf.pas',
  RGSTextureStorage in '..\RGSTextureStorage.pas',
  uRawRGBIO in 'uRawRGBIO.pas',
  uStreamUtils in '..\uStreamUtils.pas',
  GSDLLTexCommon in '..\GSDLLTexCommon.pas';

const
  RGSClass: TRGSTextureStorageClass = TRawRGBTexture;

{$E sti}

function infoproc(infoPtr: PXPluginInfo): boolean; stdcall; export;
begin
  try
    with infoPtr^ do begin
      wPluginVer := $A;
      ePluginType := xptTexture;
      ePluginIOCaps := [xioRead,xioWrite];
      ssFileExt := '.rgb;.rgba';
      ssPluginDesc := 'Sample raw RGB(A) Texture plugin';
    end;
    Result := true;
  except result := false; end;
end;

function textureSupported(data: pointer; dataSize: cardinal): cardinal; stdcall; export;
var pss: TPointerStream;
begin
  Result := 0;
  if not assigned(data) then
    Result := RGSClass.SupportBufferSize
  else begin
    pss := TPointerStream.Create(data, dataSize);
    try
      if (RGSClass.Supported(pss)) then
        Result := 1
      else Result := 0;
    finally pss.Free; end;
  end;
end;

function textureLoad(memory: boolean; data: Pointer; dataSize: cardinal): pointer; stdcall; export;
var rgs: TRGSTextureStorage;
    res: boolean;
begin
  res := false; Result := nil;
  rgs := RGSClass.Create;
  if not memory then
    res := rgs.LoadFromFile(strPas(data))
  else res := rgs.LoadFromMemory(data, dataSize);
  if not (res) then begin
    result := nil;
    rgs.Free;
  end else
    result := rgs;
end;

function textureCreate(const W,H,D: word; const Format: integer; const mipLevels: byte): pointer; stdcall; export;
var rgs: TRawRGBTexture;
begin
  rgs := TRawRGBTexture.Create(w,h,TGSImageFormat(Format),mipLevels);
  result := rgs;
end;

exports
  // Main
  infoproc,
  // Load
  textureSupported,
  textureLoad,
  textureClose,
  textureInfo,
  textureGetBits,
  // Save
  textureCreate,
  textureSave,
  textureSetBits,
  textureSetMipBits;

end.
